home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 1 / QRZ Ham Radio Callsign Database - December 1993.iso / ucsd / packet / tcpip / amiga / asrc29k.lha / iproute.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-01-08  |  13.8 KB  |  558 lines

  1. /* Lower half of IP, consisting of gateway routines
  2.  * Includes routing and options processing code
  3.  */
  4. #include "global.h"
  5. #include "config.h"
  6. #include "mbuf.h"
  7. #include "internet.h"
  8. #include "timer.h"
  9. #include "netuser.h"
  10. #include "ip.h"
  11. #include "icmp.h"
  12. #include "iface.h"
  13. #include "trace.h"
  14. #include "rip.h"
  15.  
  16. static int16 hash_ip __ARGS((int32 addr));
  17.  
  18. struct route *Routes[32][NROUTE];    /* Routing table */
  19.  
  20. struct route R_default = {        /* Default route entry */
  21.     NULLROUTE, NULLROUTE,
  22.     0,0,0,
  23.     RIP_INFINITY        /* Init metric to infinity */
  24. };
  25.  
  26. int32 Ip_addr;
  27. static struct rt_cache Rt_cache;
  28.  
  29. /* Route an IP datagram. This is the "hopper" through which all IP datagrams,
  30.  * coming or going, must pass.
  31.  *
  32.  * "rxbroadcast" is set to indicate that the packet came in on a subnet
  33.  * broadcast. The router will kick the packet upstairs regardless of the
  34.  * IP destination address.
  35.  */
  36. int ip_route(i_iface,bp,rxbroadcast)
  37. struct iface *i_iface;    /* Input interface */
  38. struct mbuf *bp;    /* Input packet */
  39. int rxbroadcast;    /* True if packet had link broadcast address */
  40. {
  41.     struct ip ip;            /* IP header being processed */
  42.     int16 ip_len;            /* IP header length */
  43.     int16 length;            /* Length of data portion */
  44.     int32 gateway;            /* Gateway IP address */
  45.     register struct route *rp;    /* Route table entry */
  46.     struct iface *iface;        /* Output interface, possibly forwarded */
  47.     int16 offset;            /* Offset into current fragment */
  48.     int16 mf_flag;            /* Original datagram MF flag */
  49.     int strict;            /* Strict source routing flag */
  50.     char prec;            /* Extracted from tos field */
  51.     char del;
  52.     char tput;
  53.     char rel;
  54.     int16 opt_len;        /* Length of current option */
  55.     char *opt;        /* -> beginning of current option */
  56.     char *ptr;        /* -> pointer field in source route fields */
  57.     struct mbuf *tbp;
  58.  
  59.     if(i_iface != NULLIF){
  60.         ipInReceives++;    /* Not locally generated */
  61.         i_iface->iprecvcnt++;
  62.     }
  63.     if(len_p(bp) < IPLEN){
  64.         /* The packet is shorter than a legal IP header */
  65.         ipInHdrErrors++;
  66.         free_p(bp);
  67.         return -1;
  68.     }
  69.  
  70.     /* Sneak a peek at the IP header's IHL field to find its length */
  71.     ip_len = (bp->data[0] & 0xf) << 2;
  72.     if(ip_len < IPLEN){
  73.         /* The IP header length field is too small */
  74.         ipInHdrErrors++;
  75.         free_p(bp);
  76.         return -1;
  77.     }
  78.     if(cksum(NULLHEADER,bp,ip_len) != 0){
  79.         /* Bad IP header checksum; discard */
  80.         ipInHdrErrors++;
  81.         free_p(bp);
  82.         return -1;
  83.     }
  84.     /* Extract IP header */
  85.     ntohip(&ip,&bp);
  86.  
  87.     if(ip.version != IPVERSION){
  88.         /* We can't handle this version of IP */
  89.         ipInHdrErrors++;
  90.         free_p(bp);
  91.         return -1;
  92.     }
  93.  
  94.     /* Trim data segment if necessary. */
  95.     length = ip.length - ip_len;    /* Length of data portion */
  96.     trim_mbuf(&bp,length);    
  97.                 
  98.     /* If we're running low on memory, return a source quench */
  99.     if(!rxbroadcast && availmem() < Memthresh)
  100.         icmp_output(&ip,bp,ICMP_QUENCH,0,NULL);
  101.  
  102.     /* Process options, if any. Also compute length of secondary IP
  103.      * header in case fragmentation is needed later
  104.      */
  105.     strict = 0;
  106.     for(opt = ip.options; opt < &ip.options[ip.optlen];opt += opt_len){
  107.         /* Most options have a length field. If this is a EOL or NOOP,
  108.          * this (garbage) value won't be used
  109.          */
  110.         opt_len = uchar(opt[1]);
  111.  
  112.         switch(opt[0] & OPT_NUMBER){
  113.         case IP_EOL:
  114.             goto no_opt;    /* End of options list, we're done */
  115.         case IP_NOOP:
  116.             opt_len = 1;
  117.             break;        /* No operation, skip to next option */
  118.         case IP_SSROUTE:    /* Strict source route & record route */
  119.             strict = 1;    /* note fall-thru */
  120.         case IP_LSROUTE:    /* Loose source route & record route */
  121.             /* Source routes are ignored unless we're in the
  122.              * destination field
  123.              */
  124.             if(ismyaddr(ip.dest) == NULLIF)
  125.                 break;    /* Skip to next option */
  126.             if(uchar(opt[2]) >= opt_len){
  127.                 break;    /* Route exhausted; it's for us */
  128.             }
  129.             /* Put address for next hop into destination field,
  130.              * put our address into the route field, and bump
  131.              * the pointer
  132.              */
  133.             ptr = opt + uchar(opt[2]) - 1;
  134.             ip.dest = get32(ptr);
  135.             put32(ptr,locaddr(ip.dest));
  136.             opt[2] += 4;
  137.             break;
  138.         case IP_RROUTE:    /* Record route */
  139.             if(uchar(opt[2]) >= opt_len){
  140.                 /* Route area exhausted; kick back an error */
  141.                 if(!rxbroadcast){
  142.                     union icmp_args icmp_args;
  143.  
  144.                     icmp_args.pointer = IPLEN + opt - ip.options;
  145.                     icmp_output(&ip,bp,ICMP_PARAM_PROB,0,&icmp_args);
  146.                 }
  147.                 free_p(bp);
  148.                 return -1;
  149.             }
  150.             /* Add our address to the route */
  151.             ptr = opt + uchar(opt[2]) - 1;
  152.             ptr = put32(ptr,locaddr(ip.dest));
  153.              opt[2] += 4;
  154.             break;
  155.         }
  156.     }
  157. no_opt:
  158.  
  159.     /* See if it's a broadcast or addressed to us, and kick it upstairs */
  160.     if(ismyaddr(ip.dest) != NULLIF || rxbroadcast){
  161. #ifdef    GWONLY
  162.     /* We're only a gateway, we have no host level protocols */
  163.         if(!rxbroadcast)
  164.             icmp_output(&ip,bp,ICMP_DEST_UNREACH,
  165.              ICMP_PROT_UNREACH,(union icmp_args *)NULL);
  166.         ipInUnknownProtos++;
  167.         free_p(bp);
  168. #else
  169.         ip_recv(i_iface,&ip,bp,rxbroadcast);
  170. #endif
  171.         return 0;
  172.     }
  173.  
  174.     /* Packet is not destined to us. If it originated elsewhere, count
  175.      * it as a forwarded datagram.
  176.      */
  177.     if(i_iface != NULLIF)
  178.         ipForwDatagrams++;
  179.  
  180.     /* Decrement TTL and discard if zero. We don't have to check
  181.      * rxbroadcast here because it's already been checked
  182.      */
  183.     if(--ip.ttl == 0){
  184.         /* Send ICMP "Time Exceeded" message */
  185.         icmp_output(&ip,bp,ICMP_TIME_EXCEED,0,NULLICMP);
  186.         ipInHdrErrors++;
  187.         free_p(bp);
  188.         return -1;
  189.     }
  190.     /* Look up target address in routing table */
  191.     if((rp = rt_lookup(ip.dest)) == NULLROUTE){
  192.         /* No route exists, return unreachable message (we already
  193.          * know this can't be a broadcast)
  194.          */
  195.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_HOST_UNREACH,NULLICMP);
  196.         free_p(bp);
  197.         ipOutNoRoutes++;
  198.         return -1;
  199.     }
  200.     rp->uses++;
  201.  
  202.     /* Check for output forwarding and divert if necessary */
  203.     iface = rp->iface;
  204.     if(iface->forw != NULLIF)
  205.         iface = iface->forw;
  206.  
  207.     /* Find gateway; zero gateway in routing table means "send direct" */
  208.     if(rp->gateway == (int32)0)
  209.         gateway = ip.dest;
  210.     else
  211.         gateway = rp->gateway;
  212.  
  213.     if(strict && gateway != ip.dest){
  214.         /* Strict source routing requires a direct entry
  215.          * Again, we know this isn't a broadcast
  216.          */
  217.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_ROUTE_FAIL,NULLICMP);
  218.         free_p(bp);
  219.         ipOutNoRoutes++;
  220.         return -1;
  221.     }
  222.     prec = PREC(ip.tos);
  223.     del = ip.tos & DELAY;
  224.     tput = ip.tos & THRUPUT;
  225.     rel = ip.tos & RELIABILITY;
  226.  
  227.     if(ip.length <= iface->mtu){
  228.         /* Datagram smaller than interface MTU; put header
  229.          * back on and send normally
  230.          */
  231.         if((tbp = htonip(&ip,bp,0)) == NULLBUF){
  232.             free_p(bp);
  233.             return -1;
  234.         }
  235.         iface->ipsndcnt++;
  236.         return (*iface->send)(tbp,iface,gateway,prec,del,tput,rel);
  237.     }
  238.     /* Fragmentation needed */
  239.     if(ip.flags.df){
  240.         /* Don't Fragment set; return ICMP message and drop */
  241.         icmp_output(&ip,bp,ICMP_DEST_UNREACH,ICMP_FRAG_NEEDED,NULLICMP);
  242.         free_p(bp);
  243.         ipFragFails++;
  244.         return -1;
  245.     }
  246.     /* Create fragments */
  247.     offset = ip.offset;
  248.     mf_flag = ip.flags.mf;        /* Save original MF flag */
  249.     while(length != 0){        /* As long as there's data left */
  250.         int16 fragsize;        /* Size of this fragment's data */
  251.         struct mbuf *f_data;    /* Data portion of fragment */
  252.  
  253.         /* After the first fragment, should remove those
  254.          * options that aren't supposed to be copied on fragmentation
  255.          */
  256.         ip.offset = offset;
  257.         if(length + ip_len <= iface->mtu){
  258.             /* Last fragment; send all that remains */
  259.             fragsize = length;
  260.             ip.flags.mf = mf_flag;    /* Pass original MF flag */
  261.         } else {
  262.             /* More to come, so send multiple of 8 bytes */
  263.             fragsize = (iface->mtu - ip_len) & 0xfff8;
  264.             ip.flags.mf = 1;
  265.         }
  266.         ip.length = fragsize + ip_len;
  267.  
  268.         /* Duplicate the fragment */
  269.         dup_p(&f_data,bp,offset,fragsize);
  270.         if(f_data == NULLBUF){
  271.             free_p(bp);
  272.             ipFragFails++;
  273.             return -1;
  274.         }
  275.         /* Put IP header back on */
  276.         if((tbp = htonip(&ip,f_data,0)) == NULLBUF){
  277.             free_p(f_data);
  278.             free_p(bp);
  279.             ipFragFails++;
  280.             return -1;
  281.         }
  282.         /* and ship it out */
  283.         if((*iface->send)(tbp,iface,gateway,prec,del,tput,rel) == -1){
  284.             ipFragFails++;
  285.             free_p(bp);
  286.             return -1;
  287.         }
  288.         iface->ipsndcnt++;
  289.         ipFragCreates++;
  290.         offset += fragsize;
  291.         length -= fragsize;
  292.     }
  293.     ipFragOKs++;
  294.     free_p(bp);
  295.     return 0;
  296. }
  297.  
  298. /* Add an entry to the IP routing table. Returns 0 on success, -1 on failure */
  299. struct route *rt_add(target,bits,gateway,iface,metric,ttl,private)
  300. int32 target;        /* Target IP address prefix */
  301. unsigned int bits;    /* Size of target address prefix in bits (0-32) */
  302. int32 gateway;        /* Optional gateway to be reached via interface */
  303. struct iface *iface;    /* Interface to which packet is to be routed */
  304. int32 metric;        /* Metric for this route entry */
  305. int32 ttl;        /* Lifetime of this route entry in sec */
  306. char private;        /* Inhibit advertising this entry ? */
  307. {
  308.     struct route *rp,**hp;
  309.  
  310.     if(iface == NULLIF)
  311.         return NULLROUTE;
  312.  
  313.     Rt_cache.route = NULLROUTE;    /* Flush cache */
  314.  
  315.     /* Zero bits refers to the default route */
  316.     if(bits == 0){
  317.         rp = &R_default;
  318.     } else {
  319.         if(bits > 32)
  320.             bits = 32;
  321.  
  322.         /* Mask off target according to width */
  323.         target &= ~0L << (32-bits);
  324.  
  325.         /* Search appropriate chain for existing entry */
  326.         for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  327.             if(rp->target == target)
  328.                 break;
  329.         }
  330.     }
  331.     if(rp == NULLROUTE){
  332.         /* The target is not already in the table, so create a new
  333.          * entry and put it in.
  334.          */
  335.         rp = (struct route *)callocw(1,sizeof(struct route));
  336.         /* Insert at head of table */
  337.         rp->prev = NULLROUTE;
  338.         hp = &Routes[bits-1][hash_ip(target)];
  339.         rp->next = *hp;
  340.         if(rp->next != NULLROUTE)
  341.             rp->next->prev = rp;
  342.         *hp = rp;
  343.         rp->uses = 0;
  344.     }
  345.     rp->target = target;
  346.     rp->bits = bits;
  347.     rp->gateway = gateway;
  348.     rp->metric = metric;
  349.     rp->iface = iface;
  350.     rp->flags = private ? RTPRIVATE : 0;    /* Should anyone be told of this route? */
  351.     rp->timer.func = rt_timeout;  /* Set the timer field */
  352.     rp->timer.arg = (void *)rp;
  353.     rp->timer.start = (1000 * ttl) / MSPTICK;
  354.     stop_timer(&rp->timer);
  355.     start_timer(&rp->timer); /* start the timer if appropriate */
  356.  
  357.     return rp;
  358. }
  359.  
  360. /* Remove an entry from the IP routing table. Returns 0 on success, -1
  361.  * if entry was not in table.
  362.  */
  363. int rt_drop(target,bits)
  364. int32 target;
  365. unsigned int bits;
  366. {
  367.     register struct route *rp;
  368.  
  369.     Rt_cache.route = NULLROUTE;    /* Flush the cache */
  370.  
  371.     if(bits == 0){
  372.         /* Nail the default entry */
  373.         stop_timer(&R_default.timer);
  374.         R_default.iface = NULLIF;
  375.         return 0;
  376.     }
  377.     if(bits > 32)
  378.         bits = 32;
  379.  
  380.     /* Mask off target according to width */
  381.     target &= ~0L << (32-bits);
  382.  
  383.     /* Search appropriate chain for existing entry */
  384.     for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  385.         if(rp->target == target)
  386.             break;
  387.     }
  388.     if(rp == NULLROUTE)
  389.         return -1;    /* Not in table */
  390.  
  391.     stop_timer(&rp->timer);
  392.     if(rp->next != NULLROUTE)
  393.         rp->next->prev = rp->prev;
  394.     if(rp->prev != NULLROUTE)
  395.         rp->prev->next = rp->next;
  396.     else
  397.         Routes[bits-1][hash_ip(target)] = rp->next;
  398.  
  399.     free((char *)rp);
  400.     return 0;
  401. }
  402.  
  403. /* Compute hash function on IP address */
  404. static int16 hash_ip(addr)
  405. register int32 addr;
  406. {
  407.     register int16 ret;
  408.  
  409.     ret = hiword(addr);
  410.     ret ^= loword(addr);
  411.     return (int16)(ret % NROUTE);
  412. }
  413.  
  414. #ifndef    GWONLY
  415. /* Given an IP address, return the MTU of the local interface used to
  416.  * reach that destination. This is used by TCP to avoid local fragmentation
  417.  */
  418. int16 ip_mtu(addr)
  419. int32 addr;
  420. {
  421.     register struct route *rp;
  422.     struct iface *iface;
  423.  
  424.     rp = rt_lookup(addr);
  425.     if(rp == NULLROUTE || rp->iface == NULLIF)
  426.         return 0;
  427.  
  428.     iface = rp->iface;
  429.     if(iface->forw != NULLIF)
  430.         return iface->forw->mtu;
  431.     else
  432.         return iface->mtu;
  433. }
  434.  
  435. /* Given a destination address, return the IP address of the local
  436.  * interface that will be used to reach it. If there is no route
  437.  * to the destination, pick the first non-loopback address.
  438.  */
  439. int32 locaddr(addr)
  440. int32 addr;
  441. {
  442.     register struct route *rp;
  443.     struct iface *ifp;
  444.  
  445.     if(ismyaddr(addr) != NULLIF)
  446.         return addr;    /* Loopback case */
  447.  
  448.     rp = rt_lookup(addr);
  449.     if(rp != NULLROUTE && rp->iface != NULLIF)
  450.         ifp = rp->iface;
  451.     else {
  452.         for(ifp = Ifaces;ifp != NULLIF;ifp = ifp->next){
  453.             if(ifp != &Loopback)
  454.                 break;
  455.         }
  456.     }
  457.     if(ifp == NULLIF || ifp == &Loopback)
  458.         return 0;    /* No dice */
  459.  
  460.     if(ifp->forw != NULLIF)
  461.         return ifp->forw->addr;
  462.     else
  463.         return ifp->addr;
  464. }
  465. #endif
  466.  
  467. /* Look up target in hash table, matching the entry having the largest number
  468.  * of leading bits in common. Return default route if not found;
  469.  * if default route not set, return NULLROUTE
  470.  */
  471. struct route *rt_lookup(target)
  472. int32 target;
  473. {
  474.     register struct route *rp;
  475.     int bits;
  476.     int32 tsave;
  477.     int32 mask;
  478.  
  479.     /* Examine cache first */
  480.     if(target == Rt_cache.target && Rt_cache.route != NULLROUTE)
  481.         return Rt_cache.route;
  482.  
  483.     tsave = target;
  484.  
  485.     mask = ~0;    /* All ones */
  486.     for(bits = 31;bits >= 0; bits--){
  487.         target &= mask;
  488.         for(rp = Routes[bits][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  489.             if(rp->target == target){
  490.                 /* Stash in cache and return */
  491.                 Rt_cache.target = tsave;
  492.                 Rt_cache.route = rp;
  493.                 return rp;
  494.             }
  495.         }
  496.         mask <<= 1;
  497.     }
  498.     if(R_default.iface != NULLIF){
  499.         Rt_cache.target = tsave;
  500.         Rt_cache.route = &R_default;
  501.         return &R_default;
  502.     } else
  503.         return NULLROUTE;
  504. }
  505.  
  506. /* Search routing table for entry with specific width */
  507. struct route *rt_blookup(target,bits)
  508. int32 target;
  509. unsigned int bits;
  510. {
  511.     register struct route *rp;
  512.  
  513.     if(bits == 0 && R_default.iface != NULLIF)
  514.         return &R_default;
  515.  
  516.     /* Mask off target according to width */
  517.     target &= ~0L << (32-bits);
  518.  
  519.     for(rp = Routes[bits-1][hash_ip(target)];rp != NULLROUTE;rp = rp->next){
  520.         if(rp->target == target){
  521.             return rp;
  522.         }
  523.     }
  524.     return NULLROUTE;
  525. }
  526.  
  527. /* Scan the routing table. For each entry, see if there's a less-specific
  528.  * one that points to the same interface and gateway. If so, delete
  529.  * the more specific entry, since it is redundant.
  530.  */
  531. void rt_merge(trace)
  532. int trace;
  533. {
  534.     int bits,i,j;
  535.     struct route *rp,*rpnext,*rp1;
  536.  
  537.     for(bits=32;bits>0;bits--){
  538.         for(i = 0;i<NROUTE;i++){
  539.             for(rp = Routes[bits-1][i];rp != NULLROUTE;rp = rpnext){
  540.                 rpnext = rp->next;
  541.                 for(j=bits-1;j >= 0;j--){
  542.                     if((rp1 = rt_blookup(rp->target,j)) != NULLROUTE
  543.                      && rp1->iface == rp->iface
  544.                      && rp1->gateway == rp->gateway){
  545.                         if(trace > 1)
  546.                             printf("merge %s %d\n",
  547.                              inet_ntoa(rp->target),
  548.                              rp->bits);
  549.                         rt_drop(rp->target,rp->bits);
  550.                         break;
  551.                     }
  552.                 }
  553.             }
  554.         }
  555.     }
  556. }
  557.  
  558.